import 'package:baseproject/view/base/drawer.dart';
import 'package:baseproject/view/home/home.dart';
import 'package:baseproject/view/killmail/killMail.dart';
import 'package:baseproject/view/market/market.dart';
import 'package:baseproject/view/message/message.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class BasePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _BasePageState();
  }
}

class _BasePageState extends State<BasePage> {
  int _currentIndex = 0;
  var _pageController = new PageController(initialPage: 0);

  void _pageChange(int index) {
    setState(() {
      if (_currentIndex != index) {
        _currentIndex = index;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("EVE"),
            automaticallyImplyLeading: false,
            centerTitle: true,
            leading: Builder(
              builder: (context) {
                return IconButton(
                  icon: Icon(Icons.account_circle),
                  onPressed: () {
                    Scaffold.of(context).openDrawer();
                  },
                );
              },
            )),
        drawer: MyDrawer(),
        body: PageView(
          onPageChanged: _pageChange,
          controller: _pageController,
          children: <Widget>[Home(), Market(), KillMail(), Message()],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            this.pushToCamera(context);
          },
          child: Icon(
            Icons.add_a_photo,
            color: Colors.white,
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        bottomNavigationBar: BottomAppBar(
          color: Theme.of(context).accentColor,
          shape: CircularNotchedRectangle(),
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 6, 0, 6),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                GestureDetector(
                  onTap: () {
                    changeCurrentIndex(0);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.home,
                        color: getColor(0),
                      ),
                      Text(
                        "首页",
                        style: TextStyle(color: getColor(0)),
                      )
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    changeCurrentIndex(1);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.shopping_basket,
                        color: getColor(1),
                      ),
                      Text(
                        "市场",
                        style: TextStyle(color: getColor(1)),
                      )
                    ],
                  ),
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Icon(
                      Icons.add_a_photo,
                      color: Colors.transparent,
                    ),
                    Text("拍照", style: TextStyle(color: Color(0xFFEEEEEE)))
                  ],
                ),
                GestureDetector(
                  onTap: () {
                    changeCurrentIndex(2);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.mail,
                        color: getColor(2),
                      ),
                      Text(
                        "击杀",
                        style: TextStyle(color: getColor(2)),
                      )
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    changeCurrentIndex(3);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.message,
                        color: getColor(3),
                      ),
                      Text(
                        "消息",
                        style: TextStyle(color: getColor(3)),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
  }

  Color getColor(int i) {
    return _currentIndex == i ? Theme.of(context).cardColor : Color(0XFFBBBBBB);
  }

  void changeCurrentIndex(int i) {
    setState(() {
      _currentIndex = i;
    });
    _pageController.jumpToPage(i);
  }

  void pushToCamera(BuildContext context) async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);
    print(image);
  }
}
